package com.yonyou.trumpchi.model;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.yonyou.trumpchi.Constants;
import com.yonyou.trumpchi.SPUtils;
import com.yonyou.trumpchi.entity.ServiceInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author liuxiaopeng
 * @version V1.0.0
 * @time 2016/11/29
 * @describe
 */

public class ServiceModel {
    private static Gson gson = new Gson();

    /**
     * 获取服务器列表
     *
     * @return
     */
    public static List<ServiceInfo> getServiceList(Context mContext) {
        Map<String, String> allService = (Map<String, String>) SPUtils.getAll(mContext);
        List<ServiceInfo> data = new ArrayList();
        Set<String> allKey = allService.keySet();
        for (String key : allKey) {
            String value=allService.get(key);
            Log.e("1",key+"========================="+value);
            ServiceInfo info;
            if (!TextUtils.isEmpty(value)) {
                info = gson.fromJson(value, ServiceInfo.class);
                data.add(info);
            }
        }
        return data;
    }


    /**
     * 将其他服务设置为 不自动登录
     *
     * @param mContext 上下文
     * @param url      将要自动登录的URL
     */
    public static void setOtherAuto(Context mContext, String url) {
        List<ServiceInfo> data = getServiceList(mContext);
        for (ServiceInfo info : data) {
            if (!url.equals(info.getUrl())) {
                info.getParams().put(Constants.SP_AUTO, "0");
                SPUtils.put(mContext, info.getUrl(), gson.toJson(info));
            }
        }
    }


    public static void del(Context mContext, String url) {
        SPUtils.remove(mContext,url);
    }

    public static void add(Context mContext,ServiceInfo info) {
        SPUtils.put(mContext,info.getUrl(),gson.toJson(info));
    }
}
